package cn.yanjr.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.yanjr.common.bean.BeanFactory;
import cn.yanjr.common.utils.StringUtils;
import cn.yanjr.system.dao.SysMenuDao;
import cn.yanjr.system.dao.SysRoleDao;
import cn.yanjr.system.dao.SysUserDao;
import cn.yanjr.system.entity.SysMenu;
import cn.yanjr.system.entity.SysRole;
import cn.yanjr.system.service.SysRoleService;

public class SysRoleServiceImpl implements SysRoleService {
	
	private SysRoleDao sysRoleDao = BeanFactory.getBean("sysRoleDao", SysRoleDao.class);
	private SysMenuDao sysMenuDao = BeanFactory.getBean("sysMenuDao", SysMenuDao.class);

	@Override
	public Map<String, Object> queryPage(Map<String, Object> param) {
		int pageNo = param.get("pageNo") == null ? -1 : Integer.parseInt(param.get("pageNo").toString());
		int pageSize = param.get("pageSize") == null ? 10 : Integer.parseInt(param.get("pageSize").toString());
		String rolename = param.get("rolename") == null ? "" : param.get("rolename").toString();
		String remarks = param.get("remarks") == null ? "" : param.get("remarks").toString();

		StringBuilder sql = new StringBuilder();
		sql
		.append("select r.*,d.label dataScope,u1.name createBy, u2.name updateBy            ")
		.append("from sys_role r                                                            ")
		.append("left join sys_user u1 on r.create_by=u1.id                                 ")
		.append("left join sys_user u2 on r.create_by=u2.id                                 ")
		.append("left join sys_dict d on r.data_scope=d.value and d.type='sys_data_scope'   ")
		.append("where r.del_flag=0                                                         ")
		;
		if(StringUtils.isNotEmpty(rolename)) {
			sql.append(" and r.name like '%").append(rolename).append("%' ");
		}
		if(StringUtils.isNotEmpty(remarks)) {
			sql.append(" and r.remarks like '%").append(remarks).append("%' ");
		}
		sql.append(" order by r.create_date desc ");
		Map<String, Object> m = new HashMap<String, Object>();
		Map<String, Object> data = sysRoleDao.findPage(sql.toString(), pageNo, pageSize);

		m.put("code", 0);
		m.put("msg", "查询成功！");
		m.put("totalpage", data.get("total"));
		m.put("page", pageNo);
		m.put("count", data.get("records"));
		m.put("data", data.get("rows"));
		return m;
	}
	

	@Override
	public SysRole get(String id) {
		return sysRoleDao.findOne(id);
	}

	@Override
	public Map<String, SysMenu> getMenusByRoleId(String id) {
		List<SysMenu> list = sysMenuDao.getMenusByRoleId(id);
		Map<String, SysMenu> result = new HashMap<String, SysMenu>();
		if(list != null && list.size() > 0) {
			for(SysMenu m : list) {
				result.put(m.getId(), m);
			}
		}
		return result;
	}


	@Override
	public Map<String, Object> saveRoleMenu(String roleId, String[] menus) {
		sysMenuDao.deleteRoleMenuByRoleId(roleId);
		String sql = "insert into sys_role_menu (role_id, menu_id) values(:1,:2)";
		List<String[]> param = new ArrayList<String[]>();
		if(menus != null && menus.length > 0) {
			for(String s : menus) {
				String[] p = new String[2];
				p[0] = roleId;
				p[1] = s;
				param.add(p);
			}
		}
		
			sysRoleDao.batchUpdate(sql, param.toArray());
		return null;
	}


	@Override
	public int save(SysRole role) {
		return sysRoleDao.save(role);
	}


	@Override
	public Map<String, Object> delete(String id) {
		sysMenuDao.deleteRoleMenuByRoleId(id);
		SysRole role = sysRoleDao.findOne(id);
		role.setDelFlag(1);
		role.setUpdateDate(new Date());
		sysRoleDao.save(role);
		return null;
	}


	@Override
	public List<SysRole> getAll() {
		return sysRoleDao.getAll();
	}


	@Override
	public List<SysRole> getUserRoles(String userId) {
		return sysRoleDao.getUserRoles(userId);
	}


	@Override
	public List<String> getUserRoleNames(String userId) {
		List<String> roleNames = new ArrayList<>();
		// TODO Auto-generated method stub
		List<SysRole> roles = sysRoleDao.getUserRoles(userId);
		if(roles != null && roles.size() > 0) {
			for(SysRole r : roles) {
				roleNames.add(r.getName());
			}
		}
		return roleNames;
	}

}
